<?php
/**
* Letterman Newsletter Component
* 
* @package Letterman
* @author Soeren
* @copyright Soeren Eberhardt <soeren@virtuemart.net>
*   (who just needed an easy and *working* Newsletter component for Mambo 4.5.1 and mixed up Newsletter and YaNC)
* @copyright Mark Lindeman <mark@pictura-dp.nl> 
*    (parts of the Newsletter component by Mark Lindeman; Pictura Database Publishing bv, Heiloo the Netherland)
* @copyright Adam van Dongen <adam@tim-online.nl>
*    (parts of the YaNC component by Adam van Dongen, www.tim-online.nl)
*    
* @copyright Philip Sylvain <contact@sphilip.com>
*  	(MVC refactorization and AJAX)
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
*
*/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();

jimport('joomla.application.component.controller');

class LettermanController extends JController
{	
	
    /**
	 * Custom Constructor
	 */
	function LettermanController( $default = array() )
	{
		// Set a default view if none exists
		if ( ! JRequest::getCmd( 'view' ) ) {
			
			JRequest::setVar('view', 'default' );
		}
		
		$this->addModelPath( JPATH_COMPONENT_ADMINISTRATOR.DS.'models' );
		
		parent::__construct( $default );
	}
	
	function subscribe ()
	{	
		$subscriber = $this->getModel('subscribers');
		
		if ( $subscriber->save() ) {
			
			jimport('joomla.utilities.utility');
			
			$database =& JFactory::getDBO();
			$mainframe =& JFactory::getApplication(); 
			
			//send confirmation email
			$siteName = $mainframe->getCfg('sitename');
			$subscriberhash = md5($database->insertid());
			$config =& JFactory::getConfig();
			$live_site = $config->getValue('config.live_site');
			
			$confirmLink = JURI::base() . 'index.php?option=com_letterman&task=confirm&subscriber=' . $subscriberhash ;
			$subject = JText::sprintf('SUBSCRIBE_SUBJECT', $siteName);
			$message = JText::sprintf('SUBSCRIBE_MESSAGE', 
			                          JRequest::getVar('name'), 
			                          $siteName, $confirmLink , 
			                          $siteName );
			                          
			$reciptient = JRequest::getVar('email'); //already valided in model
			$from = $mainframe->getCfg('mailfrom');
			
			$sendOk = JUtility::sendMail($from, $siteName, $reciptient ,$subject , $message, false);
			$sendOk = true;
			if ($sendOk) {
				JRequest::setVar('name', '');
				JRequest::setVar('email', '');
				$mainframe->enqueueMessage(JText::_('SUBSCRIBE_DESC_OK'));
				
			} else {
				JError::raiseWarning( 200, JText::_('ERROR_SENDING_SUBSCRIBE') );
			}
		} else {
		
			$this->display();
		}
	}
	
	function unsubscribe()
	{
		$subscriber = $this->getModel('subscribers');
		
		if ( $subscriber->removeFromEmail( JRequest::getVar('email', '') ) ) {
			
			jimport('joomla.utilities.utility');
			$mainframe =& JFactory::getApplication(); 
			
			//send confirmation email
			$siteName = $mainframe->getCfg('sitename');
			$subject = JText::sprintf('UNSUBSCRIBE_SUBJECT', $siteName);
			$message = JText::sprintf('UNSUBSCRIBE_MESSAGE', JRequest::getVar('name'), $siteName , $siteName );
			                          
			$reciptient = JRequest::getVar('email'); //already valided in model
			$from = $mainframe->getCfg('mailfrom');
			
			JUtility::sendMail($from, $siteName, $reciptient ,$subject , $message, false);
			$mainframe->enqueueMessage(JText::_('SUCCESS_UNSUBSCRIBE'));
			
		} else {
		
			$this->display();
		}		
		
	}
	
	function confirm ()
	{
		$database =& JFactory::getDBO();
		$mainframe =& JFactory::getApplication();
		$subscriber = $database->Quote(JRequest::getVar('subscriber', ''));
		
		$database->setQuery( 'SELECT id FROM #__letterman_subscribers WHERE md5(id) = ' . $subscriber );
		
		if ( $id = $database->loadResult() ){
        	$database->setQuery( 'UPDATE #__letterman_subscribers SET confirmed=1 WHERE id=' . $id );
        	
        	if (!$database->query()){
        		JError::raiseWarning( 200, $database->getErrorMsg() );
        	} else {
        		
        		$mainframe->enqueueMessage(JText::_('SUCCESS_CONFIRMATION'));
        	}
		} else {
			
			JError::raiseWarning( 200, JText::_('ACCOUNT_NOTFOUND') );
		}
	}
	
	/*function display($tpl='')
	{
		
		
		$document =& JFactory::getDocument();

		$viewType	= $document->getType();
		$viewName	= JRequest::getCmd( 'view', $this->getName() );

		$view = & $this->getView( $viewName, $viewType, '', array( 'base_path'=>$this->_basePath));

		// Get/Create the models
		if ($model = & $this->getModel( 'Subscribers' )) {
			// Push the default model into the view
			$view->setModel($model, true);
		}
		
		parent::display($tpl);
	}*/
	
	
}
